package com.frozenlab.android.weather.helper;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;

import android.util.Log;

import com.frozenlab.android.weather.activity.R;
import com.frozenlab.android.weather.vo.WeatherVO;

public class YahooWeatherHelper {
	public static final String TAG = "YahooWeatherHelper";
	
	private static final String TAG_WOEID = "woeid";

	private static final String TAG_YWEATHER_LOCATION = "yweather:location";
	private static final String TAG_YWEATHER_UNITS = "yweather:units";
	private static final String TAG_YWEATHER_WIND = "yweather:wind";
	private static final String TAG_YWEATHER_ATMOSPHERE = "yweather:atmosphere";
	private static final String TAG_YWEATHER_ASTRONOMY = "yweather:astronomy";
	private static final String TAG_YWEATHER_CONDITION = "yweather:condition";
	private static final String TAG_YWEATHER_FORECAST = "yweather:forecast";

	private static final String NODE_CITY = "city";
	private static final String NODE_REGION = "region";
	private static final String NODE_COUNTRY = "country";
	private static final String NODE_TEMPERATURE = "temperature";
	private static final String NODE_DISTANCE = "distance";
	private static final String NODE_PRESSURE = "pressure";
	private static final String NODE_SPEED = "speed";
	private static final String NODE_CHILL = "chill";
	private static final String NODE_DIRECTION = "direction";
	private static final String NODE_HUMIDITY = "humidity";
	private static final String NODE_VISIBILITY = "visibility";
	private static final String NODE_RISING = "rising";
	private static final String NODE_SUNRISE = "sunrise";
	private static final String NODE_SUNSET = "sunset";
	private static final String NODE_TEXT = "text";
	private static final String NODE_CODE = "code";
	private static final String NODE_TEMP = "temp";
	private static final String NODE_DATE = "date";
	private static final String NODE_DAY = "day";
	private static final String NODE_LOW = "low";
	private static final String NODE_HIGH = "high";
	
	// Yahoo weather images
	public static final int[][] weatherImageArray = {
		{R.drawable.weather_0, 0},
		{R.drawable.weather_1, 1},
		{R.drawable.weather_2, 2},
		{R.drawable.weather_3, 3},
		{R.drawable.weather_4, 4},
		{R.drawable.weather_5, 5},
		{R.drawable.weather_6, 6},
		{R.drawable.weather_7, 7},
		{R.drawable.weather_8, 8},
		{R.drawable.weather_9, 9},
		{R.drawable.weather_10, 10},
		{R.drawable.weather_11, 11},
		{R.drawable.weather_12, 12},
		{R.drawable.weather_13, 13},
		{R.drawable.weather_14, 14},
		{R.drawable.weather_15, 15},
		{R.drawable.weather_16, 16},
		{R.drawable.weather_17, 17},
		{R.drawable.weather_18, 18},
		{R.drawable.weather_19, 19},
		{R.drawable.weather_20, 20},
		{R.drawable.weather_21, 21},
		{R.drawable.weather_22, 22},
		{R.drawable.weather_23, 23},
		{R.drawable.weather_24, 24},
		{R.drawable.weather_25, 25},
		{R.drawable.weather_26, 26},
		{R.drawable.weather_27, 27},
		{R.drawable.weather_28, 28},
		{R.drawable.weather_29, 29},
		{R.drawable.weather_30, 30},
		{R.drawable.weather_31, 31},
		{R.drawable.weather_32, 32},
		{R.drawable.weather_33, 33},
		{R.drawable.weather_34, 34},
		{R.drawable.weather_35, 35},
		{R.drawable.weather_36, 36},
		{R.drawable.weather_37, 37},
		{R.drawable.weather_38, 38},
		{R.drawable.weather_39, 39},
		{R.drawable.weather_40, 40},
		{R.drawable.weather_41, 41},
		{R.drawable.weather_42, 42},
		{R.drawable.weather_43, 43},
		{R.drawable.weather_44, 44},
		{R.drawable.weather_45, 45},
		{R.drawable.weather_46, 46},
		{R.drawable.weather_47, 47},
		{R.drawable.weather_3200, 3200},
	};
	
	
	/***************************************************************************
	 * Construct URL for request Yahoo Weather Web Service
	 * @param woeid
	 * @author Winson
	 * @return url
	 **************************************************************************/
	public static String createUrl(String woeid) {
		//contruct url to use yahoo weather web services to get weather forecast
		//example: http://weather.yahooapis.com/forecastrss?w=[WOEID]
		String strUrl = YahooDataConstant.YAHOO_WEATHER_BASE_URI;
		strUrl = strUrl + "w=" + woeid;
			
		return strUrl;
	}

	public static WeatherVO parseYahooWeatherInfo(Document doc) {
		//verify parameter
		if (doc == null){
			Log.e(TAG,"Document is NULL");
			return null;
		}
		
		String city= null;
		String region= null;
		String country = null;
		String temperatureUnit= null;
		String distanceUnit= null;
		String pressureUnit= null;
		String speedUnit= null;
		String chill= null;
		String direction= null;
		String speed= null;
		String humidity= null;
		String visibility= null;
		String pressure= null;
		String rising= null;
		String sunrise= null;
		String sunset= null;
		String conditionText = null;
		String conditionCode = null;
		String conditionTemperature = null;
		String conditionDate = null;
		String forecastDay = null;
		String forecastDate = null;
		String forecastLow = null;
		String forecastHigh= null;
		String forecastText = null;
		String forecastCode = null;
		
		
		WeatherVO weatherVO = new WeatherVO();
		
		try {
			Element root = doc.getDocumentElement();
			root.normalize();

			NamedNodeMap locationNode = root.getElementsByTagName(TAG_YWEATHER_LOCATION).item(0).getAttributes();

			if (locationNode != null) {
				city = locationNode.getNamedItem(NODE_CITY).getNodeValue();
				region = locationNode.getNamedItem(NODE_REGION).getNodeValue();
				country = locationNode.getNamedItem(NODE_COUNTRY).getNodeValue();

				weatherVO.setCity(city);
				weatherVO.setRegion(region);
				weatherVO.setCountry(country);
			}

			NamedNodeMap unitsNode = root.getElementsByTagName(TAG_YWEATHER_UNITS).item(0).getAttributes();

			if (unitsNode != null) {
				temperatureUnit = unitsNode.getNamedItem(NODE_TEMPERATURE).getNodeValue();
				distanceUnit = unitsNode.getNamedItem(NODE_DISTANCE).getNodeValue();
				pressureUnit = unitsNode.getNamedItem(NODE_PRESSURE).getNodeValue();
				speedUnit = unitsNode.getNamedItem(NODE_SPEED).getNodeValue();

				weatherVO.setTemperatureUnit(temperatureUnit);
				weatherVO.setDistanceUnit(distanceUnit);
				weatherVO.setPressureUnit(pressureUnit);
				weatherVO.setSpeedUnit(speedUnit);
			}

			NamedNodeMap windNode = root.getElementsByTagName(TAG_YWEATHER_WIND).item(0).getAttributes();

			if (windNode != null) {
				chill = windNode.getNamedItem(NODE_CHILL).getNodeValue();
				direction = windNode.getNamedItem(NODE_DIRECTION).getNodeValue();
				speed = windNode.getNamedItem(NODE_SPEED).getNodeValue();

				weatherVO.setChill(chill);
				weatherVO.setDirection(direction);
				weatherVO.setSpeed(speed);
			}

			NamedNodeMap atmosphereNode = root.getElementsByTagName(TAG_YWEATHER_ATMOSPHERE).item(0).getAttributes();

			if (atmosphereNode != null) {
				humidity = atmosphereNode.getNamedItem(NODE_HUMIDITY).getNodeValue();
				visibility = atmosphereNode.getNamedItem(NODE_VISIBILITY).getNodeValue();
				pressure = atmosphereNode.getNamedItem(NODE_PRESSURE).getNodeValue();
				rising = atmosphereNode.getNamedItem(NODE_RISING).getNodeValue();

				weatherVO.setHumidity(humidity);
				weatherVO.setVisibility(visibility);
				weatherVO.setPressure(pressure);
				weatherVO.setRising(rising);
			}

			NamedNodeMap astronomyNode = root.getElementsByTagName(TAG_YWEATHER_ASTRONOMY).item(0).getAttributes();

			if (astronomyNode != null) {
				sunrise = astronomyNode.getNamedItem(NODE_SUNRISE).getNodeValue();
				sunset = astronomyNode.getNamedItem(NODE_SUNSET).getNodeValue();

				weatherVO.setSunrise(sunrise);
				weatherVO.setSunset(sunset);
			}
			
			NamedNodeMap conditionNode = root.getElementsByTagName(TAG_YWEATHER_CONDITION).item(0).getAttributes();

			if (conditionNode != null) {

				
				conditionText = conditionNode.getNamedItem(NODE_TEXT).getNodeValue();
				conditionCode = conditionNode.getNamedItem(NODE_CODE).getNodeValue();
				conditionTemperature = conditionNode.getNamedItem(NODE_TEMP).getNodeValue();
				conditionDate = conditionNode.getNamedItem(NODE_DATE).getNodeValue();

				weatherVO.setConditionText(conditionText);
				weatherVO.setConditionCode(conditionCode);
				weatherVO.setConditionTemperature(conditionTemperature);
				weatherVO.setConditionDate(conditionDate);
			}
			
			NamedNodeMap forecastNode = root.getElementsByTagName(TAG_YWEATHER_FORECAST).item(0).getAttributes();

			if (forecastNode != null) {
				forecastDay = forecastNode.getNamedItem(NODE_DAY).getNodeValue();
				forecastDate = forecastNode.getNamedItem(NODE_DATE).getNodeValue();
				forecastLow = forecastNode.getNamedItem(NODE_LOW).getNodeValue();
				forecastHigh = forecastNode.getNamedItem(NODE_HIGH).getNodeValue();
				forecastText = forecastNode.getNamedItem(NODE_TEXT).getNodeValue();
				forecastCode = forecastNode.getNamedItem(NODE_CODE).getNodeValue();		

			}
		} catch (Exception e) {
			Log.e(TAG, e.toString());
			return null;
		}

		return weatherVO;
	}
}
